package com.zhudeshuai.zhudemolist.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.zhu.zhuCore.utils.LogUtil;
import com.zhudeshuai.zhudemolist.R;
import com.zhudeshuai.zhudemolist.outgoing.ReadLog;

import java.lang.reflect.Method;

/**
 * 去电状态监听
 *
 * @author zhudeshuai
 * @since 16/6/24
 */
public class OutGoingActivity extends Activity {
    private static final int EVENT_PRECISE_CALL_STATE_CHANGED = 101;
    private static final java.lang.String TAG = "OutGoingActivity";

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            switch (what) {
                case EVENT_PRECISE_CALL_STATE_CHANGED:
//                    cm.getActiveFgCall().getState() == Call.State.ALERTING
                    try {
                        Method getActiveFgCall = mCallManagerClass.getDeclaredMethod("getActiveFgCall");
                        Object invoke = getActiveFgCall.invoke(mCallManagerClass.newInstance());
                        Method getState = invoke.getClass().getDeclaredMethod("getState");
                        Object state = getState.invoke(invoke);
                        LogUtil.d(TAG, "去电状态：" + state.toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };
    private Class<?> mCallManagerClass;
    private Object mCallManagerObj;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_out_going);
        new ReadLog(this).start();
    }

    public void start(View view) {
        try {
            if (mCallManagerClass == null) {
                mCallManagerClass = Class.forName("com.android.internal.telephony.CallManager");
                /*public static CallManager getInstance() {
                    return INSTANCE;
                }*/
                if (mCallManagerObj == null) {
                    Method getInstance = mCallManagerClass.getDeclaredMethod("getInstance");
                    mCallManagerObj = getInstance.invoke(null);
                }
            }
//            public void registerForPreciseCallStateChanged(Handler h, int what, Object obj){
            Method registerCallStateChanged = mCallManagerClass.getDeclaredMethod("registerForPreciseCallStateChanged", Handler.class, int.class, Object.class);
            if (registerCallStateChanged != null) {
                registerCallStateChanged.invoke(mCallManagerObj, mHandler, EVENT_PRECISE_CALL_STATE_CHANGED, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void open(Activity activity) {
        activity.startActivity(new Intent(activity, OutGoingActivity.class));
    }

}
